from kazoo.client import KazooClient

zk = KazooClient(hosts='192.168.1.22:2181')
zk.start()
print(zk.command(b"ruok"))



#CRUD
# Ensure a path, create if necessary
if not zk.exists("/my1"):
    zk.create("/my1", b"my1 test")
elif not zk.exists("/my1/favorite1"):
    zk.create("/my1/favorite1", b"my1 favorite1 test")

zk.ensure_path("/my1/favorite1")

# Create a node with data
if not zk.exists("/my1/favorite1/node"):
    zk.create("/my1/favorite1/node", b"a value")
if not zk.exists("/my1/favorite1/node2"):
    zk.create("/my1/favorite1/node2", b"node 2")

# Determine if a node exists
if zk.exists("/my1/favorite1"):
    print("/my1/favorite1 存在")
    # Do something

# Print the version of a node and its data
data, stat = zk.get("/my1/favorite1")
print("Version: %s, data: %s" % (stat.version, data.decode("utf-8")))

# List the children
children = zk.get_children("/my1/favorite1")
print("There are %s children with names %s" % (len(children), children))

zk.set("/my1/favorite1", b"some data")

data, stat = zk.get("/my1/favorite1")
print("Version: %s, data: %s" % (stat.version, data.decode("utf-8")))

zk.delete("/my1/favorite1/node", recursive=True)

# List the children
children = zk.get_children("/my1/favorite1")
print("There are %s children with names %s" % (len(children), children))

zk.stop()